package com.zhiyou100.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.model.Customer;
import com.zhiyou100.model.Order;
import com.zhiyou100.model.ResultObject;
import com.zhiyou100.model.Room;
import com.zhiyou100.service.CustomerService;
import com.zhiyou100.service.OrderService;
import com.zhiyou100.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    RoomService roomService;
    @Autowired
    CustomerService customerService;
    @RequestMapping("/list")
    public String listOrder(@RequestParam Map map, @RequestParam(defaultValue = "1") int pageNum, HttpServletRequest request){
        System.out.println("OrderController-map : "+map);
        PageHelper.startPage(pageNum, 5);
        List<Order> orders = orderService.listOrder(map);
        PageInfo<Order> pageInfo = new PageInfo<Order>(orders);
        request.setAttribute("map",map);
        request.setAttribute("orders",orders);
        request.setAttribute("pageInfo",pageInfo);
        return "forward:/behind/order/listOrder.jsp";
    }

    /**
     *添加订单信息
     * 首先登记客户信息 选择房间类型 及 房间号
     * 然后接收Customer Order
     * new Date set到Customer Order
     * 将然后接收Customer的客户名字及房间id  set到Order
     * 分别添加Customer Order
     *
     */
    @RequestMapping("/addOrder")
    public String addOrder(@RequestParam(defaultValue="0")String strId,String updateCustomerId ,Order order, Customer customer){
        int id = Integer.parseInt(strId);
        if(id == 0){
            System.out.println("添加的订单是 : "+order);
            System.out.println("添加的客户是 : "+customer);
            String endtime = customer.getEndtime();
            String starttime = customer.getStarttime();
            Integer roomid = customer.getRoomid();
            Date date = new Date();
            SimpleDateFormat sim1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String createTime = sim1.format(date);
            int i1 = customerService.addCustomer(customer);
            System.out.println("添加客户返回的受影响行数是   : "+i1);
            System.out.println("customer.getIdcard() - "+customer.getIdcard());
            int CustomerId =  customerService.findCustomerByIDCard(customer.getIdcard());
            System.out.println("CustomerId - "+CustomerId);
            int i3 =  roomService.updateRoomStatus(roomid,starttime,endtime);
            System.out.println("添加订单时 - 将客户预定的客房状态更新为已预订:"+i3);
            order.setOrderCustomerId(CustomerId);
            order.setOrderCreateTime(createTime);
            order.setOrderRoomId(customer.getRoomid());
            order.setOrderName(customer.getName());
            System.out.println("添加的order : "+order);
            int i = orderService.addOrder(order);
            System.out.println("添加订单返回的受影响行数是   : "+i);
        }else {
            int parseInt = Integer.parseInt(updateCustomerId);
            System.out.println("要更新的OrderId是 : "+id);
            System.out.println("要更新的CustomerId是 : "+parseInt);
            System.out.println("更新的订单是 : "+order);
            System.out.println("更新的客户是 : "+customer);
            String endtime = customer.getEndtime();
            String starttime = customer.getStarttime();
            Integer roomid = customer.getRoomid();
            order.setId(id);
            customer.setId(parseInt);//将客户id set到customer 中
            order.setOrderRoomId(customer.getRoomid());//将房间id set到订单信息中
            order.setOrderName(customer.getName());//将客户姓名 set到订单信息中
            int i3 =  roomService.updateRoomStatus(roomid,starttime,endtime);
            System.out.println("更新订单 - 将客户预定的客房状态更新为已预订:"+i3);
            int  i  = orderService.updateOrder(order);
            int i1 = customerService.updateCustomer(customer);
            System.out.println("更新Order返回的受影响行数是   : "+i);
            System.out.println("更新客户返回的受影响行数是   : "+i1);
        }
        return "redirect:/order/list.do";

    }

    /**
     *更新订单及客户信息
     * 页面传订单id和客户姓名
     * 后台根据客户姓名查询客户信息  订单id查询订单信息
     * 返回给更新页面
     */
    @RequestMapping("/lookOrder2")
    public String lookOrder2(int id,int orderCustomerId ,HttpServletRequest request) throws ParseException {
        System.out.println("要查看修改的id是 : "+id);
        System.out.println("要查看修改的orderCustomerId是:"+orderCustomerId);
        Order order =  orderService.lookOrder(id);
        Customer customer =  customerService.findCustomerById(orderCustomerId);
        System.out.println("/lookOrder2 - findCustomerById -"+customer);
        request.setAttribute("order",order);
        request.setAttribute("customer",customer);
        return "forward:/behind/order/addOrder.jsp";

    }

    @RequestMapping("/deleteAll")
    public String deleteAll(int[] ids){
        // System.out.println("要删除的数组是   :   "+ids.length);
        List<Integer> orderIds = new ArrayList<>();
        for (int id:ids) {
            orderIds.add(id);
        }
        // System.out.println("list集合的大小是  : "+videoIds.size());
        int i = orderService.deleteAll(orderIds);
        System.out.println("批量删除后返回的受影响行数是   :   "+i);
        return "redirect:/order/list.do";
    }
    @RequestMapping("/delete")
    private String delete(int id){
        System.out.println("要删除的id是    :    "+id);
        int i = orderService.delete(id);
        System.out.println("单个删除后返回的受影响行数是   :   "+i);
        return "redirect:/order/list.do";

    }
    @RequestMapping("/ajaxFindRoomNum")
    @ResponseBody
    public ResultObject ajaxFindRoomNum(String roomType){
        System.out.println("客户选择的客房类型是roomType : "+roomType );
        List<Room> rooms = roomService.findRoomByType(roomType);
        for (Room room:rooms) {
            System.out.println("room : "+room);
        }
        return new ResultObject("2000","OK",rooms);
    }

    /**
     * 退房
     * new Date 作为退房时间
     * 根据订单id将订单更新   endTime和将是否退回押金修改成已退回
     */
    @RequestMapping("/updateOrderEndTime")
    public String updateOrderEndTime(int id,int rid,int cid){
        System.out.println("要退房的订单id是 :"+id);
        Date date = new Date();
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sim2 = new SimpleDateFormat("yyyy-MM-dd");
        String endTime = sim.format(date);
        String CustomerEndtime = sim2.format(date);
        int i =  orderService.updateOrderEndTime(id,endTime);
        int i1 = roomService.updateRoomStatus2(rid);
        int i2 = customerService.updateCustomerEndTime(CustomerEndtime,cid);
        System.out.println("退房i1 :"+i+"- i2:"+i1+" -i2 :"+i2);
        return "redirect:/order/list.do";
    }

}
